/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import entities.Commande;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Film;
import entities.Utilisateur;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
import session.GestionnaireDeFilms;
import session.GestionnaireUtilisateurs;
import entities.Commande;
import session.GestionnaireCommande;
import session.GestionnaireStock;

/**
 *
 * @author asus
 */
@WebServlet(name = "ServletPanier", urlPatterns = {"/ServletPanier"})
public class ServletPanier extends HttpServlet {

    @EJB
    private GestionnaireDeFilms gestionnaireDeFilms;
    @EJB
    private GestionnaireUtilisateurs gestionnaireUtilisateurs;
    @EJB
    private GestionnaireCommande gestionnaireCommande;
    @EJB
    private GestionnaireStock gestionnaireStock;
    private List<Film> panier = new ArrayList<Film>();
    private Film filmPanier = null;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        String action = request.getParameter("action");
        String titre = request.getParameter("titre");
        String id = request.getParameter("id");
        String connecte = request.getParameter("connecte");
        String mail = request.getParameter("mail");
        String prixDVD = request.getParameter("prixDVD");
        int montant = 0;
        try {
            if (action.equals("ajouter")) {
                filmPanier = gestionnaireDeFilms.findFilmById(id);
                // Collection<Film> l = gestionnaireDeFilms.findFilmsByTitle(titre);
                if (filmPanier != null) {
                    // ajout du film dans la liste panier
                    panier.add(filmPanier);

                    session.setAttribute("ajoutPanier", panier);
//                    Collection<Film> liste = gestionnaireDeFilms.getAllFilms();
//                        request.setAttribute("listeDesFilms", liste);

                    for (Film f : panier) {
                        montant += f.getPrixDVD();
                    }
                    session.setAttribute("montant", montant);
                    RequestDispatcher dp = request.getRequestDispatcher("panier.jsp?action=panier");
                    dp.forward(request, response);
                } else {
                    System.out.println("je ne trouve pas le film");
                }
            } else if (action.equals("panier")) {
                RequestDispatcher dp = request.getRequestDispatcher("panier.jsp");
                dp.forward(request, response);
            } else if (action.equals("supprimerPanier")) {
                panier.clear();
                RequestDispatcher dp = request.getRequestDispatcher("panier.jsp?action=panier");
                dp.forward(request, response);
            } //si validerPanier { calculer montantTotal, creer cde, mettre a 0 panier, diminuer stock } 
            else if (action.equals("panierValide")) {
                if (connecte == "true") {
                    request.getRequestDispatcher("logIn.jsp").forward(request, response);
                } else {
                    Date d = new Date();
                    long date = d.getTime();

                    for (Film f : panier) {
                        montant += f.getPrixDVD();
                    }
                    Utilisateur u = gestionnaireUtilisateurs.getUserByMail(mail);
                    gestionnaireCommande.creeCommande(date, u, montant);
                    panier.clear();
                    RequestDispatcher dp = request.getRequestDispatcher("commande.jsp?action=panierValide");
                    dp.forward(request, response);
                }
            } else if (action.equals("continuerAchat")) {
                RequestDispatcher dp = request.getRequestDispatcher("index.jsp?action=listerFilms");
                dp.forward(request, response);
            }


        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
